home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_common.idb / usr / freeware / bin / fixpath.z / fixpath
Text File  |  2002-10-07  |  7KB  |  305 lines

  1.  
  2. eval 'exec perl -S $0 "$@"'
  3.     if 0;
  4.  
  5. # fixpath - add/remove paths from startup files.
  6. # $Id: fixpath,v 1.2 1999/01/14 20:11:16 drk Exp $
  7. #
  8. &main;
  9. exit 0;
  10.  
  11. sub main
  12. {
  13.     @files = ();
  14.     $shell = $ENV{'SHELL'};
  15.     $home = $ENV{'HOME'};
  16.     @info = getpwuid($>);
  17.     if ($#info == -1) {
  18.         die "$0: can't find passwd entry for UID $>\n";
  19.     }
  20.     $home = $info[7] unless ($home ne "");
  21.     $shell = $info[8] unless ($shell ne "");
  22.  
  23.     # Make sure we are doing the right thing.
  24.     open(TTY, "/dev/tty") || die "$0: No tty";
  25.     select(STDOUT);
  26.     $| = 1;
  27.     $ok = 0;
  28.     while (!$ok) {
  29.         do {
  30.             print "Your shell seems to be $shell, is that correct? ";
  31.             $ans = <TTY>;
  32.             print "Please use yes/no for your answer.\n"
  33.                 unless $ans =~ /^[yYnN]/;
  34.         } while ($ans !~ /^[yYnN]/);
  35.         if ($ans =~ /^[yY]/) {
  36.             $ok = 1;
  37.         } else {
  38.             print "What is your shell? ";
  39.             $shell = <TTY>;
  40.             chop($shell);
  41.         }
  42.     }
  43.  
  44.     # XXX - don't handle zsh - couldn't find a man page.
  45.     $shell =~ s|.*/||;
  46.     $ok = 0;
  47.     foreach $s ('sh', 'ksh', 'bash', 'csh', 'tcsh') {
  48.         $ok = 1 if $s eq $shell;
  49.     }
  50.     if (!$ok) {
  51.         die "$0: I don't know what to do for your '$shell' shell.\n";
  52.     }
  53.  
  54.     # Depending on which shell it is, figure out the file.
  55.     if ($shell eq "sh") {
  56.         push(files, "$home/.profile");
  57.         $default = "$home/.profile";
  58.     }
  59.  
  60.     if ($shell eq "csh") {
  61.         foreach $f ('.login', '.cshrc') {
  62.             if (-e "$home/$f" && &haspath("$home/$f")) {
  63.                 push(files, "$home/$f");
  64.             }
  65.         }
  66.         if ($#files == -1) {
  67.             push(files, "$home/.login");
  68.         }
  69.         $default = "$home/.login";
  70.     }
  71.  
  72.     if ($shell eq "tcsh") {
  73.         foreach $f ('.login', '.tcshrc', '.cshrc') {
  74.             if (-e "$home/$f" && &haspath("$home/$f")) {
  75.                 push(files, "$home/$f");
  76.             }
  77.         }
  78.         if ($#files == -1) {
  79.             push(files, "$home/.login");
  80.         }
  81.         $default = "$home/.login";
  82.     }
  83.  
  84.     if ($shell eq "ksh") {
  85.         foreach $f ('.kshrc', '.profile') {
  86.             if (-e "$home/$f" && &haspath("$home/$f")) {
  87.                 push(files, "$home/$f");
  88.             }
  89.         }
  90.         if ($#files == -1) {
  91.             push(files, "$home/.profile");
  92.         }
  93.         $default = "$home/.profile";
  94.     }
  95.  
  96.     if ($shell eq "bash") {
  97.         foreach $f ('.bashrc', '.bash_profile', '.bash_login', '.profile') {
  98.             if (-e "$home/$f" && &haspath("$home/$f")) {
  99.                 push(files, "$home/$f");
  100.             }
  101.         }
  102.         if ($#files == -1) {
  103.             push(files, "$home/.profile");
  104.         }
  105.         $default = "$home/.profile";
  106.     }
  107.  
  108.     # OK, now we have set up the following:
  109.     # $shell is the users shell.
  110.     # @files is the list of files that had one or more of the PATH things.
  111.     #warn "shell = $shell\nfiles = @files\n";
  112.  
  113.     foreach $f (@files) {
  114.         &fixpath($f);
  115.     }
  116.     if (!$path) {
  117.         &addpath("PATH", '${PATH}:/usr/freeware/bin', $default);
  118.     }
  119.     if (!$manpath) {
  120.         &addpath("MANPATH", '/usr/share/catman:/usr/share/man:/usr/catman:/usr/man:/usr/freeware/catman:/usr/freeware/man', $default);
  121.     }
  122.     if (!$X) {
  123.         &addpath("XFILESEARCHPATH", '/usr/lib/X11/%T/%N:/usr/freeware/lib/X11/%T/%N', $default);
  124.     }
  125. }
  126.  
  127. sub addpath
  128. {
  129.     local($which, $p, $file) = @_;
  130.  
  131.     print "Adding new variable $which to $file\n";
  132.     if ($shell =~ /csh/) {
  133.         system("echo setenv $which '$p' >> $file");
  134.     } else {
  135.         system("echo $which='$p' >> $file");
  136.         system("echo export $which >> $file");
  137.     }
  138. }
  139.  
  140. sub fixpath
  141. {
  142.     open(F, $f) || die "$0: can't open $f\n";
  143.     open(N, ">/tmp/new$$");
  144.     while (<F>) {
  145.         if (/^\s*PATH=/ || /XFILESEARCHPATH=/ ||
  146.             /MANPATH=/ || /set\s+path\s*=\s*\(/ ||
  147.             /setenv\s+PATH\s+/ || /setenv\s+MANPATH\s+/ ||
  148.             /setenv\s+XFILESEARCHPATH/) {
  149.             $manpath = 1 if /MANPATH/;
  150.             $X = 1 if /XFILESEARCHPATH/;
  151.             $path = 1 if /^\s*PATH=/ ||
  152.                 /set\s+path\s*=\s*\(/ || /setenv\s+PATH\s+/;
  153.             chop;
  154.             do {
  155.                 $new = &fixit($f, $_);
  156.             } while ($new eq "");
  157.             $_ = "$new\n";
  158.         }
  159.         print N;
  160.     }
  161.     close(N);
  162.     print "\nDiff of $f vs. new:\n";
  163.     system("diff $f /tmp/new$$");
  164.     do {
  165.         print "Use new file? ";
  166.         $ans = <TTY>;
  167.         print "Please answer y/n" unless $ans =~ /^[yYnN]/;
  168.     } while ($ans !~ /^[yYnN]/);
  169.     if ($ans =~ /^[yY]/) {
  170.         system("cp $f $f.bak");
  171.         system("cp /tmp/new$$ $f");
  172.         print "Relogin or source $f to have changes take effect.\n";
  173.         print "$f.bak is the previous version of your file.\n";
  174.     }
  175.     unlink("/tmp/new$$");
  176. }
  177.  
  178. sub fixit
  179. {
  180.     local($file, $line) = @_;
  181.  
  182.     if ($line =~ /^\s*PATH=/) {
  183.         $var = "PATH";
  184.         $add = "/usr/freeware/bin";
  185.     } elsif ($line =~ /XFILESEARCHPATH=/) {
  186.         $var = "XFILESEARCHPATH";
  187.         $add = "/usr/freeware/lib/X11/%T/%N"; # XXX? check this.
  188.     } elsif ($line =~ /MANPATH=/) {
  189.         $var = "MANPATH";
  190.         $add = "/usr/freeware/catman:/usr/freeware/man";
  191.     } elsif ($line =~ /set\s+path\s*=\s*\(/) {
  192.         $var = "path";
  193.         $add = "/usr/freeware/bin";
  194.     } elsif ($line =~ /setenv\s+PATH\s+/) {
  195.         $var = "PATH";
  196.         $add = "/usr/freeware/bin";
  197.     } elsif ($line =~ /setenv\s+MANPATH\s+/) {
  198.         $var = "MANPATH";
  199.         $add = "/usr/freeware/catman:/usr/freeware/man";
  200.     } elsif ($line =~ /setenv\s+XFILESEARCHPATH\s+/) {
  201.         $var = "XFILESEARCHPATH";
  202.         $add = "/usr/freeware/lib/X11/%T/%N"; # XXX? check this.
  203.     }
  204.  
  205.     # This is really tacky, but I want to catch multi line paths.
  206.     while ($line =~ m|.*\s*\\\s*$|) {
  207.         $_ = <F>;
  208.         $line .= $_;
  209.     }
  210.     $save = $line;
  211.  
  212.     do {
  213.         #print "\nIn $file, your $var variable is:\n";
  214.         #print "\n$line\n";
  215.         if ($line =~ "$add") {
  216.             $q = "\nRemove $add from \$$var in $file? ";
  217.         } else {
  218.             $q = "\nAdd $add to \$$var in $file? ";
  219.         }
  220.         print $q;
  221.         $ans = <TTY>;
  222.         print "Please use yes/no for your answer.\n"
  223.             unless $ans =~ /^[yYnN]/;
  224.     } while ($ans !~ /^[yYnN]/);
  225.     if ($ans !~ /^[yY]/) {
  226.         print "OK, skipping this one.\n";
  227.         return $save;
  228.     }
  229.  
  230.     # Remove case.
  231.     if ($line =~ m|$add|) {
  232.         if ($line =~ m|:$add|) {
  233.             $line =~ s|:$add||;
  234.         } elsif ($line =~ m|$add:|) {
  235.             $line =~ s|$add:||;
  236.         } else {
  237.             $line =~ s|$add||;
  238.         }
  239.         return $line;
  240.     }
  241.  
  242.     # Add case.
  243.     $start = $stop = "";
  244.     @line = ();
  245.     if ($var eq "path") {
  246.         $line =~ /(.*set\s+path\s*=\s*\()(.*)(\))/;
  247.         $join = " "; $start = $1; $line = $2; $stop = " $3";
  248.     } elsif ($line =~ /setenv/) {
  249.         $line =~ /(.*setenv\s+$var\s+)(.*)/;
  250.         $join = ":"; $start = $1; $line = $2; $stop = "";
  251.     } else {
  252.         if ($line =~ /(\"\s*$)/) {
  253.             $stop = '"';
  254.             $line =~ s/\"\s*$//;
  255.         }
  256.         $line =~ /(.*$var=\"?)(.*)/;
  257.         $join = ":"; $start = $1; $line = $2;
  258.     }
  259.     @line = split(/[:\s]/, $line);
  260.     #print "<$start> <$line> <$stop>\n";
  261.     do {
  262.         print "\nYour $var is: @line\n";
  263.         $n = $#line + 1;
  264.         print "You can put $add anywhere in this path.\n";
  265.         print "To put it first, type '1',\n";
  266.         print "to put it last, type '\$',\n";
  267.         print "and to put it in the middle, type 2 for second, 3 for fourth, etc.\n";
  268.         print "Your $var currently has $n paths, 1..$n.\n";
  269.         print "\nPut $add where? ";
  270.         chop($ans = <TTY>);
  271.     } while ($ans !~ /^[\$1-9]/);
  272.     if ($ans eq '$') {
  273.         push(line, "$add");
  274.     } else {
  275.         @new = ();
  276.         $ans--;
  277.         # XXX - check for out of bounds!
  278.         for ($i = 0; $i < $ans; $i++) {
  279.             push(new, shift(line));
  280.         }
  281.         push(new, "$add");
  282.         push(new, @line);
  283.         @line = @new;
  284.     }
  285.     $line = $start . join($join, @line) . $stop;
  286.     return $line;
  287. }
  288.  
  289. # Look for a path/PATH variable.  Leaves F open, but it gets reused.
  290. sub haspath
  291. {
  292.     local($f) = @_;
  293.  
  294.     open(F, $f) || die "$0: can't open $f\n";
  295.     while (<F>) {
  296.         # sh types
  297.         return 1 if /^\s*PATH=/ || /XFILESEARCHPATH=/ || /MANPATH=/;
  298.  
  299.         # csh types
  300.         return 1 if /set\s+path\s*=\s*\(/ || /setenv\s+PATH\s+/ ||
  301.             /setenv\s+MANPATH\s+/ || /setenv\s+XFILESEARCHPATH/;
  302.     }
  303.     return 0;
  304. }
  305.